#!/usr/bin/perl

use strict;
use warnings;

use LWP;

my $site = 'http://www.asiaforum.no/memberlist.php?start=';
my $repository = 'repository.members';

my $debug = 0;

# leser inn db
my %newsitems = ();
open(REP, "<$repository") || die "Can not open $repository";
while(<REP>) {
	next if /^#/;
	chop;
	my($url,$title) = split(/-->>/);
	next if $url =~ /^\s*$/;
	next if $title =~ /^\s*$/;
	$newsitems{$url} = $title;
}
close(REP);


# Finne alle medlemmer og behandle dem

my $browser = LWP::UserAgent->new;
for(my $i=0;$i<4000;$i+=50) {
	my $memberpage = $site . $i;
	print STDERR "Henter $memberpage...\n" if $debug;
	my $response = $browser->get($memberpage);
	die "Can't get $memberpage -- ", $response->status_line unless $response->is_success;
	my $content = $response->content;

	print STDERR "Tolker html\n" if $debug;
	while($content =~ /<tr>(.*?)<\/tr>/sg) {
		my $rows = $1;
		my $line = 0;
		my $id = "";
		my $nick = "";
		my $place = "";
		while($rows =~ /<td class="row(\d+)\" align="center".*?>(.+?)<\/td>/sg) {
			my $cell = $2;
			$cell =~s/&nbsp;//;			
			if($line == 2) {
				$cell =~ /<a href=\"profile.php\?mode=viewprofile&amp;u=(\d+)?.*\" class=\"gen\">(.+)<\/a><\/span>/;
				$id = $1;
				$nick = $2;
			}
			if($line == 4) {
				$cell =~ /<span class=\"gen\">(.+)<\/span>/;
				$place = $1 ? $1 : "";
			}
			$line++;
		}
		$newsitems{'http://asiaforum.no/profile.php?mode=viewprofile&u=' . $id} = "$nick: $place" if $id;
	}	 
	print STDERR "done\n" if $debug;
}

print STDERR "Lager db\n" if $debug;
open(REP, ">$repository") || die "Can not open $repository";
foreach my $url (sort keys %newsitems) {
	next if $url =~ /^\s*$/;
	print REP $url . "-->>" . $newsitems{$url} . "\n";
}
close(REP);
